package com.xyz.keshe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xyz.keshe.R;
import com.xyz.keshe.model.News;

import java.util.List;

public class NewsAdapter extends ArrayAdapter<News> {

    private Context context;
    private List<News> newsList;

    public NewsAdapter(@NonNull Context context, List<News> newsList) {
        super(context, R.layout.item_news, newsList);
        this.context = context;
        this.newsList = newsList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
            holder = new ViewHolder();
            holder.tvTitle = convertView.findViewById(R.id.tv_title);
            holder.tvCategory = convertView.findViewById(R.id.tv_category);
            holder.tvDate = convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        News news = newsList.get(position);
        holder.tvTitle.setText(news.getTitle());
        holder.tvCategory.setText(news.getCategoryName());
        holder.tvDate.setText(news.getCreateTime());

        return convertView;
    }

    private static class ViewHolder {
        TextView tvTitle;
        TextView tvCategory;
        TextView tvDate;
    }
} 